<extend name="Public:base" />
<block name="content">
	

    <div class="alert alert-danger hidden"><i class="fa fa-exclamation-circle"></i><span id="error"></span> 
      <button type="button" class="close">&times;</button>
    </div>

	
<div class="page-header">
		<h1>
			{$breadcrumb2}
			<small>
				<i class="icon-double-angle-right"></i>
				{$crumbs}
			</small>
			<button name="send"  type="submit" style="float:right;"  class="btn button btn-sm btn-primary">提交</button>
			
		</h1>
	</div>
<div class="row">
	<div class="col-xs-12">	
		
		<div class="form-horizontal">
			<notempty name="Think.get.id">
			<input name="id" type="hidden" value="{$Think.get.id}" />
			</notempty>
			
			<div class="form-group">
				<label class="col-sm-2 control-label"> * 选项名称 </label>
			<div class="col-sm-10">
				<div class="clearfix">
					<input name="name" id="name" class="col-xs-10 col-sm-5"  value="{$option.name|default=''}" type="text">
				</div>
			</div>			 
			</div>
			
			
			
			<div class="form-group">
				<label class="col-sm-2 control-label"> * 类型 </label>
				<div class="col-sm-4">							
					<select class="form-control" name="type">
						<option <?php if($option['type']=='select'){echo ' selected="selected"';} ?> value="select">下拉列表(单选)</option>
						<option <?php if($option['type']=='radio'){echo ' selected="selected"';} ?> value="radio">单选组</option>
						<option <?php if($option['type']=='checkbox'){echo ' selected="selected"';} ?> value="checkbox">多选组</option>
						
					</select>
				</div>
			</div>
			
			<table id="option-value" class="table table-striped table-bordered table-hover">
				<thead>
					<tr>
						<td class="text-left required">选项值：</td>						
						<td class="text-left">  排序：</td>
						<td></td>
					</tr>
				</thead>
				<tbody>
					<?php $option_value_row = 1; ?>
					<?php if(isset($option_values)){ ?>
					<?php foreach ($option_values as $option_value) { ?>
		              <tr id="option-value-row<?php echo $option_value_row; ?>">
		                <td class="text-left">
		                	<input type="text" name="option_value[<?php echo $option_value_row; ?>][name]" value="<?php echo $option_value['value_name']; ?>" />
		                </td>
		               
		                <td class="text-right"><input type="text" name="option_value[<?php echo $option_value_row; ?>][sort_order]" value="<?php echo $option_value['value_sort_order']; ?>" class="form-control" /></td>
		                <td class="text-left"><button type="button" onclick="$('#option-value-row<?php echo $option_value_row; ?>').remove();" data-toggle="tooltip"  class="btn btn-danger"><i class="icon-trash"></i></button></td>
		              </tr>
		              <?php $option_value_row++; ?>
		              <?php } ?>
		              <?php } ?>
				</tbody>
			</table>
			<div>
				<a class="add_image btn btn-primary " onclick="addOptionValue();">添加选项值</a>
			</div>
			
		</div>
	</div>
</div>
</block>
<block name="javascript">

<script>
var option_value_row = '<?php echo $option_value_row; ?>';

function addOptionValue() {
	html  = '<tr id="option-value-row' + option_value_row + '">';		
	html += '<td class="text-left">';
	html += '<div><input type="text" name="option_value[' + option_value_row + '][name]" value=""  class="form-control" />';
    html += '</div>';	
	html += '</td>';
	html += '  <td class="text-right"><input type="text" name="option_value[' + option_value_row + '][sort_order]" value="' + option_value_row + '" class="form-control" /></td>';
	html += '  <td class="text-left"><button type="button" onclick="$(\'#option-value-row' + option_value_row + '\').remove();" data-toggle="tooltip"  class="btn btn-danger"><i class="icon-trash"></i></button></td>';
	html += '</tr>';	
	
	$('#option-value tbody').append(html);
	
	option_value_row++;
}


$('.button').click(function(){
	$.ajax({
		url: '{$action}',
		type: 'post',
		data: $('input[type=\'text\'],select,input[type=\'hidden\']'),
		dataType: 'json',
		beforeSend: function() {
			$('.button').attr('disabled', true);			
			$('.button').after('<span class="wait">&nbsp;<img src="__IMG__/loading.gif" alt="" /></span>');
		},	
		complete: function() {
			$('.button').attr('disabled', false); 
			$('.wait').remove();
		},			
		success: function(json) {
			
			$('.alert-danger').addClass('hidden');
					
			if (json['redirect']) {
				
				location = json['redirect']
								
			} else if (json['error']) {		
							
				$('.alert-danger').removeClass('hidden');							
				$('#error').text(json['error']);																																			
			}  
		},
		error: function(xhr, ajaxOptions, thrownError) {
			alert('修改失败');
		}
	});		
});
$('.close').click(function(){
		$(this).parent().addClass('hidden');
	});
</script>							
</block>